//
//  RZQFindViewController.m
//  LastOne
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "RZQFindViewController.h"
#import "EFAnimationViewController.h"
#import "FitHeader.h"
#import "RZQFindMoreCollectionCell.h"
#import "RZQCuriousViewController.h"
#import "HTTPMethod.h"
#import "MoreModel.h"
#import "RZQPartLongViewController.h"
#import "RZQScanViewController.h"
#import "RZQSeacherViewController.h"
#import "NightHeader.h"
#import "GiFHUD.h"
@interface RZQFindViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) EFAnimationViewController *viewController;

@property(nonatomic, strong)UIView *moreView;

@property(nonatomic, assign)NSInteger flag;

@property(nonatomic, strong)UICollectionView *moreCollectionView;

@property(nonatomic, strong)NSMutableArray *MoreArray;
@end

@implementation RZQFindViewController
-(void)dealloc {
    
    [_viewController.view removeFromSuperview];
    [_viewController removeFromParentViewController];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"日报";
 
    
    self.viewController = ({
        EFAnimationViewController *viewController = [[EFAnimationViewController alloc] init];
        [self.view addSubview:viewController.view];
        [self addChildViewController:viewController];
        [viewController didMoveToParentViewController:self];
        viewController;
    });
    _flag = 0;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"gengduo"] style:UIBarButtonItemStylePlain target:self action:@selector(moreAction)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"find"] style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];


    
    
}
#pragma mark 创建视图控制器
-(void)moreAction
{
    [self getNet];
    if (_flag == 0) {
        _moreView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
      
        _moreView.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:_moreView];
        UICollectionViewFlowLayout *moreFlowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        moreFlowLayout.itemSize = CGSizeMake(100 * FitWidth, 80 * FitHeight);
        moreFlowLayout.minimumLineSpacing = 10 * FitWidth;
        moreFlowLayout.sectionInset = UIEdgeInsetsMake(50 * FitWidth, 20 * FitHeight, 0, 25 * FitHeight);
        _moreCollectionView = [[UICollectionView  alloc]initWithFrame:CGRectMake(0, 60 * FitHeight, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:moreFlowLayout];
        _moreCollectionView.delegate = self;
        _moreCollectionView.dataSource = self;
        _moreCollectionView.backgroundColor = [UIColor whiteColor];
        [_moreView addSubview:_moreCollectionView];
        
        [_moreCollectionView registerClass:[RZQFindMoreCollectionCell class] forCellWithReuseIdentifier:@"moreIdentifier"];
        
        [_moreCollectionView NightWithType:UIViewColorTypeNormal];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(90 * FitWidth, 20 * FitHeight , 200 * FitWidth, 40 * FitHeight)];
        [_moreView NightWithType: UIViewColorTypeNormal];
        label.backgroundColor = [UIColor redColor];
        label.text = @"我的收藏";
        label.textAlignment = 1;
        label.layer.cornerRadius = 15;
        label.clipsToBounds = YES;
        [label NightWithType:UIViewColorTypeNormal];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(scanAction)];
        label.userInteractionEnabled = YES;
        [label addGestureRecognizer:tap];
        
        
        [_moreView addSubview:label];
         [GiFHUD setGifWithImageName:@"loading.gif"];
        
        
        _flag = 1;
    }
    else if (_flag == 1)
    {
        _moreView.hidden = YES;
        _flag = 0;
    }
    
    
    
}
#pragma mark 搜索页面


-(void)searchAction
{
    self.hidesBottomBarWhenPushed = YES;
    RZQSeacherViewController *rzq = [[RZQSeacherViewController alloc]init];
    [self.navigationController pushViewController:rzq animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
-(void)scanAction
{
    RZQScanViewController *rzq = [[RZQScanViewController alloc]init];
    [self.navigationController pushViewController:rzq animated:YES];
    
    
}
#pragma mark 网络请求
-(void)getNet
{
    [GiFHUD show];
    [HTTPMethod getDataByString:@"http://app.qdaily.com/app/homes/left_sidebar" BodyDic:nil WithDataBlock:^(id data) {
        NSArray *array = [data objectForKey:@"response"];
        _MoreArray = [NSMutableArray array ];
        for (NSDictionary *dic in array) {
            MoreModel *more = [[MoreModel alloc]init];
            [more setValuesForKeysWithDictionary:dic];
            [_MoreArray addObject:more];
        }
        [_moreCollectionView reloadData];
        [GiFHUD dismiss];
    }];
}
#pragma mark 界面设置
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    RZQFindMoreCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"moreIdentifier" forIndexPath:indexPath];
    
   
        cell.num = indexPath.row ;

    
    
    return cell;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 12;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        RZQCuriousViewController *rzqCur = [[RZQCuriousViewController alloc]init];
        [self.navigationController pushViewController:rzqCur animated:YES];
    }
    if (indexPath.row > 0) {
     
        RZQPartLongViewController *rzqLong = [[RZQPartLongViewController alloc]init];
        [self.navigationController pushViewController:rzqLong animated:YES];
        rzqLong.index = [_MoreArray[indexPath.row - 1]Id];
        
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
